package com.hui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.mapper.StoreCategoryMapper;
import com.hui.entity.dao.StoreCategory;
import com.hui.entity.enums.ShopCommonEnum;
import com.hui.entity.vo.StoreCategoryVo;
import com.hui.service.StoreCategoryService;
import com.hui.util.BeanCopyUtils;
import com.hui.util.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类表(StoreCategory)表服务实现类
 *
 * @author hui
 * @since 2022-07-13 12:27:07
 */
@Service("storeCategoryService")
public class StoreCategoryServiceImpl extends ServiceImpl<StoreCategoryMapper, StoreCategory> implements StoreCategoryService {

    //TODO 可以用steam流优化一下
    @Override
    public ResponseResult getList() {
        LambdaQueryWrapper<StoreCategory> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreCategory::getIsShow, ShopCommonEnum.SHOW_1.getValue()) //查找显示的
                .orderByAsc(StoreCategory::getSort);
        List<StoreCategory> list = list(wrapper); //查询出所有的分类
        //进行分组，分为一级和二级分类
        List<StoreCategory> Level_one = new ArrayList<>();
        List<StoreCategory> Level_two = new ArrayList<>();
        for (StoreCategory cate : list) {
            if (cate.getPid() == 0) {
                //一级
                Level_one.add(cate);
            } else {
                Level_two.add(cate);
            }
        }
        //将分类拷贝到Vo中
        List<StoreCategoryVo> voOne = BeanCopyUtils.copyBeanList(Level_one, StoreCategoryVo.class);
        List<StoreCategoryVo> voTwo = BeanCopyUtils.copyBeanList(Level_two, StoreCategoryVo.class);
        //将二级分类加入到一级分类
        for (StoreCategoryVo vo : voOne) {
            Integer oneId = vo.getId();
            for (StoreCategoryVo two : voTwo) {
                if (two.getPid() == oneId) {
                    vo.getChildren().add(two);
                }
            }
        }
        return ResponseResult.okResult(voOne);
    }
}

